//
//  NSString+CycleDealNum.m
//  CycleControlFunction
//
//  Created by ddy on 2019/5/10.
//  Copyright © 2019年 cycle. All rights reserved.
//

#import "NSString+CycleDealNum.h"

@implementation NSString (CycleDealNum)
/**
 * 防止掉精度,比如临界值4.495,当doubleValue后会变成4.494999999,导致四舍五入成4.49,正确应该是4.50
 * @param numStr : 后台取到的数字字符串
 */
+ (NSString *)cycle_returnAccurateNum:(NSString *)numStr {
    /*
     typedef NS_ENUM(NSUInteger, NSRoundingMode) {
     NSRoundPlain,   // Round up on a tie(四舍五入)
     NSRoundDown,    // Always down == truncate(只舍不入)
     NSRoundUp,      // Always up(只入不舍)
     NSRoundBankers  // on a tie round so last digit is even(也是四舍五入,这是和NSRoundPlain不一样,如果精确的哪位是5,
     它要看精确度的前一位是偶数还是奇数,如果是奇数,则入,偶数则舍,例如scale=1,表示精确到小数点后一位, NSDecimalNumber 为1.25时,
     NSRoundPlain结果为1.3,而NSRoundBankers则是1.2),下面是例子:
     };
     * scale表示精确到小数点后几位,并且NSRoundingMode影响的也是scale位
     * raiseOnExactness/raiseOnOverflow/raiseOnUnderflow raiseOnDivideByZero
     分别表示数据精确/上溢/下益/除以0时是否以异常处理,一般情况下都设置为NO
     */
    NSDecimalNumberHandler *hander = [[NSDecimalNumberHandler alloc]initWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
    //四舍五入
    NSDecimalNumber *inputDecimal = [NSDecimalNumber decimalNumberWithString:numStr];
    NSDecimalNumber *outPutDecimal = [inputDecimal decimalNumberByRoundingAccordingToBehavior:hander];
    return [outPutDecimal stringValue];
}
@end
